package com.ruoyi.quartz.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.constant.ScheduleConstants;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * 调度任务执行类：com.ruoyi.iot.domain.Command.sendCommand()
 */
@Slf4j
@Data
public class IotJob {

    private Long id;

    @NotBlank(message = "任务名称不能为空")
    private String name;

    private String productKey;

    private String channelKey;

    private String deviceKey;

    // 示例：{ "Power":"on", "WF":23.6 }
    private String params;

    private String cronExpression;

    private String status;

    private Long sysJobId;

    // 定时任务规则描述
    private String des;

    //0=正常,1=暂停
    private Long userId;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    public String getTarget(){
        return "commandService.sendCommand('" +
                productKey +
                "','" +
                channelKey +
                "','" +
                deviceKey +
                "','" +
                params +
                "'," +
                sysJobId +
                "L ,'" +
                "timer" +
                "')";
    }

    public SysJob ToSysJob(){
        SysJob sysJob = new SysJob();
        sysJob.setJobName(name);
        sysJob.setMisfirePolicy(ScheduleConstants.MISFIRE_DO_NOTHING);
        sysJob.setStatus(status);
        sysJob.setJobGroup("DEFAULT");
        sysJob.setConcurrent("1");
        sysJob.setCronExpression(cronExpression);
        sysJob.setInvokeTarget(getTarget());
        return sysJob;
    }

}
